<?php

include 'myspacerawler.php';

#Lets use the standard getopt to get command line options
$shortopts  = '';
$longopts  = array(
    'start:',     //start artist (mandatory)
    'end:',       //end artist (mandatory)
    'filename:',  //output file name (and path)
    'dbuser:',   //database user name (default 'root')
    'dbpasswd:', //database passwd (defaul '') 
    'dbname:',   //database name (default 'myspace')
    'dbaddress:' //database address (default 'localhost')
);

$usage = 'Usage:
--start <start position>
--end <end position>
[--filename <output text file>]
[--dbuser <databased user name>]
[--dbpasswd <database password>]
[--dbname <database name>]
[--dbaddress <database name>]';

$options = getopt($shortopts, $longopts);

if(array_key_exists('start', $options) and $options['start']) 
  { $startPosition = $options['start']; } 
else exit($usage);
if(array_key_exists('end', $options) and $options['end']) 
  { $endPosition = $options['end']; } 
else exit($usage);
if(array_key_exists('filename', $options) and $options['filename']) 
  { $fileName = $options['filename']; } 
else $fileName='crawler.txt';
if(array_key_exists('dbuser', $options) and $options['dbuser']) 
  { $dbUser = $options['dbuser']; } 
else $dbUser='root';
if(array_key_exists('dbpasswd', $options) and $options['dbpasswd']) 
  { $dbPasswd = $options['dbpasswd']; } 
else $dbPasswd='';
if(array_key_exists('dbname', $options) and $options['dbname']) 
  { $dbName = $options['dbname']; } 
else $dbName='myspace';
if(array_key_exists('dbaddress', $options) and $options['dbaddress']) 
  { $dbAddress = $options['dbaddress']; }  
else $dbAddress='localhost';

$crawler = new MySpaceCrawler($fileName, $dbName, $dbAddress, $dbUser, $dbPasswd);

$crawler->print_out("search for artists: $startPosition - $endPosition\n"); 
$artists = $crawler->getArtistsFromUnsignedCharts($startPosition,$endPosition); 
print_r($artists);


foreach($artists as $a){
	$n = 1;
	
	#Insert Artist
	$crawler->print_out("\n ************************************************************************************** ") ;
	$crawler->print_out("\n insert artist profile: "); 
	$theArtistsProfile = $crawler->getShortBasicProfileInfo($a); 
	$crawler->print_out($theArtistsProfile['friendID']. " : " . $crawler->insertProfileInCondor($theArtistsProfile,++$dataSet));
	
	$crawler->print_out("\n " . print_r($theArtistsProfile, true), true);
	
	#insert Dataset
	$crawler->print_out("\n insert dataset: " . $dataSet . "; " . $theArtistsProfile['name']);mysql_query("INSERT INTO datasets (datasetId, dataset) VALUES ('". $dataSet . "','". $theArtistsProfile['name'] . " (" . $theArtistsProfile['friendID'] .")')");
	
	#search for his/her friends
	$crawler->print_out("\n count friends: "); 
	$friends = $crawler->getAllFriendIDs($a,1); $crawler->print_out(" " . count($friends));

	#insert friends
	$crawler->print_out("\n crawling profiles:	");
	foreach($friends as $friendID){
		$result = $crawler->getShortBasicProfileInfo($friendID);
		$crawler->print_out(print_r($result, true),true);
		
		# if it's not a band
		if($result['sourceType'] != "band"){
			$crawler->insertProfileInCondor($result,$dataSet);
			$noneBandFriends[] = $result['friendID'];
		}
		
		#print progress
		$crawler->print_out(".");
		
		if($n % 50 == 0) echo $n . "\n			";$n++;
	}
	$crawler->print_out("\n non-band friends:			" . count($noneBandFriends));
	
	$crawler->print_out("\n insert friend links:			" . $crawler->insertFriendLinksInCondor($a,$noneBandFriends,$dataSet));

	unset($noneBandFriends);
}

$crawler->__destruct();
unset($crawler);

?>